﻿using com.fmtx.IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using fmtx.Framework.DALFactory;
using com.fmtx.Model;
using System.Collections;
using Newtonsoft.Json;
using fmtx.Utility;

namespace website.Areas.Action.Controllers
{
    public class ArticleController : BaseController
    {
        //
        // GET: /Action/Article/
        private IArticleType articleType = DataAccess<IArticleType>.GetObject();
        private IArticle article = DataAccess<IArticle>.GetObject();
        private IComment comment = DataAccess<IComment>.GetObject();

        /// <summary>
        /// 保存文章类型
        /// </summary>
        /// <param name="name"></param>
        /// <param name="jsoncallback"></param>
        /// <returns></returns>
        [IsLoginJson]
        public JavaScriptResult AddType(string name, string jsoncallback)
        {
            Hashtable hash = new Hashtable();
            try
            {
                if (!string.IsNullOrEmpty(name))
                {
                    ArticleType model = new ArticleType();
                    model.Uid = userInfo.Uid;
                    model.Name = name;
                    articleType.Add(model);
                    hash["error"] = 0;
                    hash["data"] = articleType.Query(userInfo.Uid);
                }
                else
                {
                    hash["error"] = 1;
                    hash["msg"] = "请输入内容！";
                }
            }
            catch (Exception ex)
            {
                fmtx.Framework.Log.Error(ex);
                hash["error"] = 1;
                hash["msg"] = "操作失败！" + ex.Message;
            }
            return JavaScript(jsoncallback + "(" + JsonConvert.SerializeObject(hash) + ")");
        }

        /// <summary>
        /// 简单查询
        /// </summary>
        /// <param name="maxid"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public ActionResult GetArticle(int maxid, int size,string keyword)
        {
            var list = ArticleCache.Query(maxid, size, keyword);
            return Json(list,JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 简单查询
        /// </summary>
        /// <param name="maxid"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [OutputCache(Duration=10)]
        public ActionResult GetHostArticle(int index, int size,string title,string keyword)
        {
            var list = article.QureyHost(index, size, title, keyword);
            var res=new List<OutPut>();
            foreach (var item in list)
            {
                res.Add(ArticleCache.BindDetail(item));
            }
            return Json(res, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 简单查询
        /// </summary>
        /// <param name="maxid"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [OutputCache(Duration = 10)]
        public ActionResult GetPraiseArticle(int index, int size, string title, string keyword)
        {
            var list = article.QureyPraise(index, size, title, keyword);
            var res = new List<OutPut>();
            foreach (var item in list)
            {
                res.Add(ArticleCache.BindDetail(item));
            }
            return Json(res, JsonRequestBehavior.AllowGet);
        }

        #region 删除文章
        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [IsLoginJson]
        public ActionResult DeleteArticle(int id)
        {
            var model = article.Get(id);
            Hashtable hash = new Hashtable();
            if (model.Uid == userInfo.Uid || IsManager)
            {
                article.Delete(id);
                hash["error"] = 0;
                ArticleCache.Remove(id);
            }
            else
            {
                hash["error"] = 1001;
            }
            return Json(hash);
        } 
        #endregion

        #region 删除文章类型
        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [IsLoginJson]
        public ActionResult DeleteArticleType(int id)
        {
            var model = articleType.Get(id);
            Hashtable hash = new Hashtable();
            if (model.Uid == userInfo.Uid || IsManager)
            {
                articleType.Delete(id);
                hash["error"] = 0;
                ArticleCache.RemoveType(id);
            }
            else
            {
                hash["error"] = 1001;
            }
            return Json(hash);
        }
        #endregion

        /// <summary>
        /// 修改封面
        /// </summary>
        /// <param name="id"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [IsLoginJson]
        public ActionResult UploadFrontCover(int id, string file)
        {
            var model = article.Get(id);
            Hashtable hash = new Hashtable();
            if (model.Uid == userInfo.Uid || IsManager)
            {
                model.FrontCover = StaticCs.SaveTempPic(file);
                model.Scale = ImageFactory.GetScale(Server.MapPath(StaticCs.VirtualPath + model.FrontCover));
                article.Update(model);
                hash["error"] = 0;
                hash["file"] = model.FrontCover;
                hash["scale"] = model.Scale;
                StaticCs.RemoveUserFolder(userInfo.Uid);
            }
            else
            {
                hash["error"] = 1001;
            }
            return Json(hash,JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取用户文章列表
        /// </summary>
        /// <param name="lastid"></param>
        /// <param name="size"></param>
        /// <param name="uid"></param>
        /// <param name="title"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public ActionResult GetUserArticles(int lastid,int size,int uid,string title,int typeid)
        {
            List<OutPut> models = new List<OutPut>();
            var list = article.Query(lastid, size, typeid, uid, title, uid == userInfo.Uid);
            foreach (var item in list)
            {
                models.Add(ArticleCache.BindDetail(item));
            }
            return Json(models, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 改变发布状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="statu"></param>
        /// <returns></returns>
        [IsLoginJson]
        [HttpPost]
        public ActionResult ChangeStatu(int id, Approved statu)
        {
            var model = article.Get(id);
            model.Statu = statu;
            var res= article.Update(model);
            if (statu == Approved.IsPass)
            {
                ArticleCache.Insert(model);
            }
            else {
                ArticleCache.Remove(model.Id);
            }
            return Json(new { error = res ? 0 : 1 });
        }

        public ActionResult UpState()
        {
            //ICity city = DataAccess<ICity>.GetObject();
            //IPlace place = DataAccess<IPlace>.GetObject();
            var list = article.Query();
            foreach (var item in list)
            {
                //item.Statu = Approved.IsPass;
                //if (item.CityId > 0 && string.IsNullOrEmpty(item.CityName))
                //{
                //    var c = city.Get(item.CityId);
                //    item.CityName = c.Name;
                //}
                //if (item.PlaceId > 0 && string.IsNullOrEmpty(item.PlaceName))
                //{
                //    var p = place.Get(item.PlaceId);
                //    item.PlaceName = p.Name;
                //}
                item.Content = WebHelper.Replace(item.Content, "<a(.*?)>", "");
                item.Content = WebHelper.Replace(item.Content, @"</[\s\t\r\n]*a[\s\t\r\n]*>", "");
                article.Update(item);
            }
            return null;
        }

    }
}
